package org.crazyit.activiti_d3._7_AuthenticatedUserId;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.impl.identity.Authentication;

public class AuthenticatedUserId {

    public static void main(String[] args) {

        //1. 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        //2. 得到身份服务组件实例
        final IdentityService identityService = engine.getIdentityService();

        //3. 设置当前线程的userId 为1
        identityService.setAuthenticatedUserId("3");

        //4. 启动两条线程
        new Thread(() -> {
            identityService.setAuthenticatedUserId("1");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程1的userId:" + Authentication.getAuthenticatedUserId());
        }).start();
        new Thread(() -> {
            identityService.setAuthenticatedUserId("2");
            System.out.println("线程2的userId:" + Authentication.getAuthenticatedUserId());
        }).start();

    }

}
